"""Lit configuration to drive test in this repo."""

# -*- Python -*-
# pylint: disable=undefined-variable

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os

import lit.formats
import lit.llvm
from lit.llvm.subst import ToolSubst
import lit.util

# Configuration file for the 'lit' test runner.
real_test_srcdir = os.environ['TEST_SRCDIR']
external_srcdir = real_test_srcdir

# name: The name of this test suite.
config.name = 'MLIR_PPHLO_OPT'
config.llvm_tools_dir = os.path.join(external_srcdir, 'llvm-project', 'llvm')
config.mlir_obj_root = os.path.join(real_test_srcdir)
config.mlir_tools_dir = os.path.join(external_srcdir, 'llvm-project', 'mlir')
# test_source_root: The root path where tests are located.
config.test_source_root = os.path.dirname(__file__)
# test_exec_root: The root path where tests should be run.
config.test_exec_root = os.path.join(
    real_test_srcdir, 'spulib', 'libspu', 'compiler', 'tests'
)
config.mlir_pphlo_tools_dir = os.path.join(
    real_test_srcdir, 'spulib', 'libspu', 'compiler', 'tools'
)

# suffixes: A list of file extensions to treat as test files.
config.suffixes = ['.mlir']

test_dir = os.environ['TEST_TARGET']
test_dir = test_dir.strip('/').rsplit(':', 1)[0]
config.mlir_test_dir = os.path.join(
    real_test_srcdir, os.environ['TEST_WORKSPACE'], test_dir
)

lit.llvm.initialize(lit_config, config)

from lit.llvm import llvm_config

config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)

config.substitutions.append(('%PATH%', config.environment['PATH']))

llvm_config.with_system_environment(['HOME', 'INCLUDE', 'LIB', 'TMP', 'TEMP'])

llvm_config.use_default_substitutions()

# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
# subdirectories contain auxiliary inputs for various tests in their parent
# directories.
config.excludes = ['Inputs', 'Examples', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt']

# Tweak the PATH to include the tools dir.
llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True)

tool_dirs = [
    config.mlir_pphlo_tools_dir,
    config.llvm_tools_dir,
]
tools = [
    'mlir-pphlo-opt',
]

llvm_config.add_tool_substitutions(tools, tool_dirs)
